
package com.bluelinelabs.conductor.demo.controllers;

import com.bluelinelabs.conductor.conductor.manager.ObjectManager;
import ohos.aafwk.content.IntentParams;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

import com.bluelinelabs.conductor.conductor.Controller;
import com.bluelinelabs.conductor.conductor.ControllerChangeHandler;
import com.bluelinelabs.conductor.conductor.RouterTransaction;
import com.bluelinelabs.conductor.conductor.changehandler.FadeChangeHandler;
import com.bluelinelabs.conductor.conductor.changehandler.HorizontalChangeHandler;
import com.bluelinelabs.conductor.conductor.changehandler.VerticalChangeHandler;
import com.bluelinelabs.conductor.demo.ResourceTable;
import com.bluelinelabs.conductor.demo.controllers.base.BaseController;


/**
 * lipeiquan
 *
 * @since 2021-05-06
 */
public class TransitionDemoController extends BaseController {


    public static final String KEY_INDEX = "TransitionDemoController.index";
    private TransitionDemo demo = TransitionDemo.values()[(int) getArgs().getParam(KEY_INDEX)];


    public TransitionDemoController(IntentParams args) {
        super(ResourceTable.Layout_controller_transition_demo, args);
    }


    @Override
    public void onViewCreated(ohos.agp.components.Component component) {
        title = "Transition Demos";
        ObjectManager.getInstance().setIdTitleAboutSetVisibility(Component.HIDE);
        if (demo.layout == TransactionDemoLayout.SHARED) {
            component.findComponentById(ResourceTable.Id_bg_view).setVisibility(Component.HIDE);
        }

        Text Id_title = (Text) component.findComponentById(ResourceTable.Id_title);
        ohos.agp.components.Component Id_bg_view = component.findComponentById(ResourceTable.Id_bg_view);

        StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig();
        layoutConfig.setMarginTop(demo.layout.topMargin);
        layoutConfig.alignment = DependentLayout.LayoutConfig.HORIZONTAL_CENTER;
        layoutConfig.width = StackLayout.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = StackLayout.LayoutConfig.MATCH_CONTENT;
        Id_title.setLayoutConfig(layoutConfig);


        if (demo.colorId != 0) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(getActivity().getString(demo.colorId))));
            Id_bg_view.setBackground(shapeElement);
        }

        int nextIndex = demo.ordinal() + 1;

        int buttonColor = 0;
        if (nextIndex < TransitionDemo.values().length) {
            buttonColor = TransitionDemo.values()[nextIndex].colorId;
        } else {
            buttonColor = ResourceTable.Color_blue_grey_300;
        }

        Image Id_next = (Image) component.findComponentById(ResourceTable.Id_next);
        ShapeElement shapeElement = new ShapeElement(getActivity(), ResourceTable.Graphic_circle);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(getActivity().getString(buttonColor))));
        Id_next.setBackground(shapeElement);

        Id_title.setText(demo.title);

        int finalNextIndex = nextIndex;
        Id_next.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(ohos.agp.components.Component component) {
                if (finalNextIndex < TransitionDemo.values().length) {
                    getRouter().pushController(getRouterTransaction(finalNextIndex, TransitionDemoController.this));
                } else {
                    getRouter().popToRoot();
                }
            }
        });

    }


    private ControllerChangeHandler getChangeHandler(Controller from) {

        ControllerChangeHandler controllerChangeHandler = null;
        switch (demo) {
            case VERTICAL:
                controllerChangeHandler = new VerticalChangeHandler();
                break;
            case CIRCULAR:
                if (from instanceof TransitionDemoController) {
                    TransitionDemoController demoController = (TransitionDemoController) from;
                    controllerChangeHandler = new CircularRevealChangeHandlerCompat(demoController.getView().findComponentById(ResourceTable.Id_next), demoController.getView());
                }

                break;
            case FADE:
                controllerChangeHandler = new FadeChangeHandler();
                break;
            case ARC_FADE:
                break;
            case ARC_FADE_RESET:
                break;
            case HORIZONTAL:
            case FLIP:
                controllerChangeHandler = new HorizontalChangeHandler();
                break;

        }
        return controllerChangeHandler;
    }


    private enum TransactionDemoLayout {

        STANDARD(96, LayoutAlignment.BOTTOM),
        SHARED(24, LayoutAlignment.CENTER);

        private int topMargin;
        private int fabGravity;

        TransactionDemoLayout(int topMargin, int fabGravity) {
            this.topMargin = topMargin;
            this.fabGravity = fabGravity;
        }
    }


    private enum TransitionDemo {

        VERTICAL(
            "Vertical Slide Animation",
            TransactionDemoLayout.STANDARD,
            ResourceTable.Color_blue_grey_300
        ),
        CIRCULAR(
            "Circular Reveal Animation",
            TransactionDemoLayout.STANDARD,
            ResourceTable.Color_red_300
        ),
        FADE("Fade Animation",
            TransactionDemoLayout.STANDARD,
            ResourceTable.Color_blue_300
        ),
        FLIP(
            "Flip Animation",
            TransactionDemoLayout.STANDARD,
            ResourceTable.Color_deep_orange_300
        ),
        HORIZONTAL(
            "Horizontal Slide Animation",
            TransactionDemoLayout.STANDARD,
            ResourceTable.Color_green_300
        ),
        ARC_FADE(
            "Arc/Fade Shared Element Transition",
            TransactionDemoLayout.SHARED,
            ResourceTable.Color_blue_grey_300
        ),
        ARC_FADE_RESET(
            "Arc/Fade Shared Element Transition",
            TransactionDemoLayout.STANDARD,
            ResourceTable.Color_pink_300
        );


        private String title;
        private TransactionDemoLayout layout;
        private int colorId;

        TransitionDemo(String title, TransactionDemoLayout layout, int colorId) {
            this.title = title;
            this.layout = layout;
            this.colorId = colorId;
        }


    }


    public static RouterTransaction getRouterTransaction(int index, Controller fromController) {
        IntentParams intentParams = new IntentParams();
        intentParams.setParam(TransitionDemoController.KEY_INDEX, index);
        TransitionDemoController toController = new TransitionDemoController(intentParams);
        return RouterTransaction.with(toController)
            .pushChangeHandler(toController.getChangeHandler(fromController))
            .popChangeHandler(toController.getChangeHandler(fromController));
    }

}
